package com.xiaoyu.lanling.feature.user.viewholder

import `in`.srain.cube.views.list.ViewHolderBase
import android.content.DialogInterface
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AlertDialog
import com.facebook.drawee.view.SimpleDraweeView
import com.xiaoyu.base.app.App
import com.xiaoyu.base.app.AppContext
import com.xiaoyu.base.app.GlobalUI
import com.xiaoyu.base.utils.extensions.getViewTag
import com.xiaoyu.base.utils.extensions.setOnClickDebounceListener
import com.xiaoyu.base.utils.extensions.setViewTag
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.event.user.UserDeletePhotoEvent
import com.xiaoyu.lanling.feature.user.model.UserAlbumItem
import com.xiaoyu.lanling.media.image.ImageViewUtil

class UserAlbumViewHolder : ViewHolderBase<UserAlbumItem>() {

    companion object {
        val sOnItemClickListener = View.OnClickListener { v ->
            val itemData = v.getViewTag<UserAlbumItem>() ?: return@OnClickListener
            val activity = App.getInstance().topActivity ?: return@OnClickListener
            val items = arrayOf(AppContext.getString(R.string.action_delete))

            AlertDialog.Builder(activity)
                    .setItems(items) { dialog, which ->
                        when (which) {
                            0 -> UserDeletePhotoEvent(itemData.url).post()
                        }
                    }
                    .show()
        }
    }

    private var mDraweeView: SimpleDraweeView? = null

    override fun createView(layoutInflater: LayoutInflater, parent: ViewGroup): View? {
        val view = layoutInflater.inflate(R.layout.item_user_profile_edit_album, parent, false)
        mDraweeView = view.findViewById(R.id.album)
        view.setOnClickDebounceListener(sOnItemClickListener)
        return view
    }

    override fun showData(position: Int, itemData: UserAlbumItem) {
        mDraweeView?.let {
            ImageViewUtil.loadImage(it, itemData.imageLoadParam)
        }
        mDraweeView?.setViewTag(itemData)
    }
}